package com.ysd.crm.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ysd.crm.dao.IUsersDao;
import com.ysd.crm.dao.UserRoleDao;
import com.ysd.crm.dto.UsersDTO;
import com.ysd.crm.model.UserRole;
import com.ysd.crm.model.Users;

@RestController
@RequestMapping("/users")
public class UsersController {

	@Resource
	private IUsersDao usersDao;
	@Resource
	private UserRoleDao userRoleDao;
	
	//登录
	@RequestMapping("/login")
	public Object login(Users user) {
		return usersDao.findByUnameAndUpass(user.getUname(), user.getUpass());
	}
	
	//分页查询
	@RequestMapping("findAll")
	public Map findAll(UsersDTO dto) {
		//准备查询条件
		Specification<Users> specification = new Specification<Users>() {
			@Override
			public Predicate toPredicate(Root<Users> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				Predicate predicate = criteriaBuilder.conjunction();//动态SQL表达式
				List<Expression<Boolean>> exList = predicate.getExpressions();//表达式集合
				if(dto.getUname()!=null&&!"".equals(dto.getUname())) {//用户名模糊查询
					exList.add(criteriaBuilder.like(root.<String>get("uname"), "%"+dto.getUname()+"%"));
				}
				if(dto.getStart()!=null) {//大于或等于起始日期
					exList.add(criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("createTime"), dto.getStart()));
				}
				if(dto.getEnd()!=null) {//小于或等于结束日期
					exList.add(criteriaBuilder.lessThanOrEqualTo(root.<Date>get("createTime"), dto.getEnd()));
				}
				return predicate;
			}
		};
		//准备分页条件
		Pageable pageable = PageRequest.of(dto.getPage()-1, dto.getLimit());
		
		Page<Users> page =  usersDao.findAll(specification, pageable);
		Map map = new HashMap();
		map.put("code", 0);
		map.put("count", 100);
		map.put("data", page.getContent());
		return map;
	}
	
	//id查询
	@RequestMapping("findById")
	public Object findById(Integer id) {
		return usersDao.findById(id);
	}
	
	//添加或更新
	@RequestMapping("save")
	public void save(Users user) {
		System.out.println(user.toString());
		usersDao.save(user);
	}
	
	//id删除
	@RequestMapping("delOne")
	public void delOne(Integer id) {
		usersDao.deleteById(id);
	}
	
	//分配角色
	@RequestMapping("/setRole")
	public void setRole(UserRole ur,String checked) {
		if(checked.equals("true")) {
			userRoleDao.save(ur);
		}else {
			int id = userRoleDao.findByUidAndRid(ur.getUid(), ur.getRid());
			userRoleDao.deleteById(id);
		}
		
	}
	
	
}
